/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lzmpt.imchat.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.lzmpt.imchat.DemoHelper;
import com.lzmpt.imchat.R;
import com.hyphenate.easeui.widget.EaseAlertDialog;
import com.lzmpt.imchat.ui.chatactivity.AddContactMsgActivity;

public class AddContactActivity extends BaseActivity {
    private EditText editText;
    private RelativeLayout searchedUserLayout;
    private TextView nameText;
    private Button searchBtn;
    private String toAddUsername;
    private ProgressDialog progressDialog;

    private ImageView mImgSearchCancle;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.em_activity_add_contact);


        initView();
        setUpViwe();
        searchedUserLayout = (RelativeLayout) findViewById(R.id.ll_user);
        mImgSearchCancle = findViewById(R.id.img_search_cancle);
        nameText = (TextView) findViewById(R.id.name);
        searchBtn = (Button) findViewById(R.id.search);


    }

    private void initView() {
        mTextView = (TextView) findViewById(R.id.add_list_friends);
        editText = (EditText) findViewById(R.id.edit_note);
        mImgSearchCancle = findViewById(R.id.img_search_cancle);

        String strAdd = getResources().getString(R.string.add_friend);
        mTextView.setText(strAdd);
        String strUserName = getResources().getString(R.string.user_name);
        editText.setHint(strUserName);
    }


    private void setUpViwe() {
        mImgSearchCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                final String name = editText.getText().toString().toLowerCase();
                String saveText = searchBtn.getText().toString();
                if (s.length() > 0) {
                    mImgSearchCancle.setVisibility(View.VISIBLE);
                } else {
                    mImgSearchCancle.setVisibility(View.INVISIBLE);
                }

                if (getString(R.string.button_search).equals(saveText)) {
                    toAddUsername = name;
                    if (TextUtils.isEmpty(name)) {
                        searchedUserLayout.setVisibility(View.GONE);
                    }

                    // TODO you can search the user from your app server here.

                    //show the userame and add button if user exist
                    searchedUserLayout.setVisibility(View.VISIBLE);
                    nameText.setText(toAddUsername);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * search contact
     *
     * @param v
     */
    public void searchContact(View v) {
        final String name = editText.getText().toString().toLowerCase();
        String saveText = searchBtn.getText().toString();

        if (getString(R.string.button_search).equals(saveText)) {
            toAddUsername = name;
            if (TextUtils.isEmpty(name)) {
                searchedUserLayout.setVisibility(View.GONE);
                new EaseAlertDialog(this, R.string.Please_enter_a_username).show();
                return;
            }

            // TODO you can search the user from your app server here.

            //show the userame and add button if user exist
            searchedUserLayout.setVisibility(View.VISIBLE);
            nameText.setText(toAddUsername);

        }
    }

    /**
     * add contact
     *
     * @param view
     */
    public void addContact(View view) {
        startActivity(new Intent(AddContactActivity.this, AddContactMsgActivity.class).putExtra("userId", toAddUsername));
    }


    public void back(View v) {
        finish();
    }
}
